package com.example.lin.health;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import com.example.lin.health.Base.BaseActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class searchActivity  extends BaseActivity implements View.OnClickListener{
    private Button searchBtn,cancelBtn,startBtn,endBtn;
    private ListView searchList;
    private Calendar calendar;
    private TextView searchText;
    private  String sport_name="";
    private List<JSONObject>  sportDatas=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        findById();
        searchBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        startBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        calendar=Calendar.getInstance();
        searchText.setText(getIntent().getStringExtra("name"));
        sport_name=getIntent().getStringExtra("name");
    }

    @Override
    public void onClick( final View v) {
        switch (v.getId()){
            case R.id.seh_search:

                 break;
            case R.id.seh_goback:
                finish();
                break;
            case R.id.seh_start_btn:
            case R.id.seh_end_btn:
                    DatePickerDialog datePickerDialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        ((Button)v).setText(year+"-"+monthOfYear+"-"+dayOfMonth);
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH )+1,calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
                break;




        }
    }
    public void findById(){
           searchBtn=(Button)findViewById(R.id.seh_search);
          cancelBtn=(Button)findViewById(R.id.seh_goback);
         startBtn=(Button)findViewById(R.id.seh_start_btn);
        endBtn=(Button)findViewById(R.id.seh_end_btn);
        searchText=(TextView)findViewById(R.id.seh_head);



    }
    private void getdata()
    {

        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                HashMap <String,Object>map=new HashMap();
                  map.put("account",APPApplication.account);
                   map.put("sport_name",sport_name );
                map.put("start_time","");
                map.put("end_time", "");
                final String result= HttpUilts.dopost("get_records.aspx", map);
                runOnUiThread(new Runnable() {
                    public void run() {
                       // @Override
                      //  toast(result);
                        try {
                            JSONObject jsonObject=new JSONObject(result);
                            if(jsonObject.getInt("code")==0)
                            {
                                   toast(jsonObject.getString("msg"));
                                JSONArray js=jsonObject.getJSONArray("data");
                                 for(int i=0;i<js.length();i++)
                                 {
                                     sportDatas.add(js.getJSONObject(i));
                                 }
                            }
                            else {
                                toast(jsonObject.getString("msg"));
                            }
                            searchList.setAdapter(new myadapter());
                        } catch (JSONException e) {



                        }
                    }
                });
            }
        };
        new Thread(runnable).start();
    }
    class  myadapter extends BaseAdapter{

        @Override
        public int getCount() {

               return sportDatas.size();
        }

        @Override
        public Object getItem(int position) {
                   return position;
        }

        @Override
        public long getItemId(int position) {
                       return  position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                         convertView=getLayoutInflater().inflate(R.layout.list,parent, false);
                        //  convertView=getLayoutInflater().
                         TextView start=(TextView)convertView.findViewById(R.id.item_start);
                         TextView  end=(TextView)convertView.findViewById(R.id.item_end);
                               // TextView
                        return convertView;
        }
    }


}
